package com.wuliupai.delivery.wuliupaidelivery.adapter;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.wuliupai.delivery.wuliupaidelivery.R;
import com.wuliupai.delivery.wuliupaidelivery.bean.WalletEntity;
import com.wuliupai.delivery.wuliupaidelivery.ui.service.StickyListHeadersAdapter;
import com.wuliupai.delivery.wuliupaidelivery.utils.LogUtils;
import com.wuliupai.delivery.wuliupaidelivery.utils.StringUtils;

import java.util.List;

/**
 * 钱包 交易记录adapter
 */
public class walletTradingRecordAdapter extends BaseAdapter implements StickyListHeadersAdapter{

    private List<WalletEntity.InfolistBean> infolist;
    private Context context;
    private String money; //钱包交易记录金额

    public walletTradingRecordAdapter(Context context , List<WalletEntity.InfolistBean> infolist) {
        this.context = context;
        this.infolist = infolist;
    }

    @Override
    public View getHeaderView(int position, View convertView, ViewGroup parent) {
        ViewHolderFragment vh_shipmentHeader;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.item_wallet_trading_record_title, null, false);
            vh_shipmentHeader = new ViewHolderFragment();
            vh_shipmentHeader.tv_wallet_trading_title_record = (TextView) convertView
                    .findViewById(R.id.tv_wallet_trading_title_record);
            convertView.setTag(vh_shipmentHeader);
        } else {
            vh_shipmentHeader = (ViewHolderFragment) convertView.getTag();
        }
        String DealTime = infolist.get(position).getSubmitTime().substring(0, 7);
        String[] split = DealTime.split("-");
        String Time = split[0] + " " + split[1] + "月";
        vh_shipmentHeader.tv_wallet_trading_title_record.setText(Time);
        return convertView;
    }

    @Override
    public String getHeaderName(int position) {
        String headerName = null;
        if (position != 0) {//2016-08-18 15:26

            String TimeStr = infolist.get(position).getSubmitTime().substring(0, 7);
            String oldTimeStr = infolist.get(position - 1).getSubmitTime().substring(0, 7);

            headerName = TimeStr.equals(oldTimeStr) ? null
                    : TimeStr;
        }
        return headerName;
    }

    @Override
    public int getCount() {
        return infolist != null ? infolist.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return infolist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolderFragment vh_shipment;
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.item_wallet_trading_record, null, false);
            vh_shipment = new ViewHolderFragment();
            vh_shipment.tv_wallet_trading_title = (TextView) convertView
                    .findViewById(R.id.tv_wallet_trading_title);
            vh_shipment.tv_wallet_trading_time = (TextView) convertView
                    .findViewById(R.id.tv_wallet_trading_time);
            vh_shipment.tv_wallet_trading_money = (TextView) convertView
                    .findViewById(R.id.tv_wallet_trading_money);
            convertView.setTag(vh_shipment);

        }else {
            vh_shipment = (ViewHolderFragment) convertView.getTag();
        }

        vh_shipment.tv_wallet_trading_title.setText(infolist.get(position).getDealObject());
        money = infolist.get(position).getMoney();
        boolean contains = money.contains("-");
        Log.i("wuliupai", "compareTo:" + contains    + "  " + money);
        if (!contains){
            if (!money .equals("0.00")){
                String paymon = StringUtils.format2Yuan(money + "");
                money = "+" + paymon;
            }
        }else {
            money.replace("-","");
            String paymon = StringUtils.format2Yuan(money + "");
            money = paymon;
        }
        vh_shipment.tv_wallet_trading_money.setText(money);//infolist.get(position).getMoney()
        if (contains){
            vh_shipment.tv_wallet_trading_money.setTextColor(Color.parseColor("#ff000000"));
        }else {
            vh_shipment.tv_wallet_trading_money.setTextColor(Color.parseColor("#15ba5b"));
        }
        vh_shipment.tv_wallet_trading_time.setText(infolist.get(position).getSubmitTime());

        return convertView;
    }


    class ViewHolderFragment {
        TextView tv_wallet_trading_title, tv_wallet_trading_time, tv_wallet_trading_money,
        tv_wallet_trading_title_record;

    }

}
